package com.zxy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zxy.constant.MessageConstant;
import com.zxy.entity.PageResult;
import com.zxy.entity.QueryPageBean;
import com.zxy.entity.Result;
import com.zxy.pojo.TravelGroup;
import com.zxy.service.TravelGroupService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/travelGroup")
public class TravelGroupController {
    @Reference
    TravelGroupService travelGroupService;
    @RequestMapping("/findAll")
    public Result findAll(){ // @RequestBody从请求体内取数据
        try {
            List<TravelGroup> lists = travelGroupService.findAll();
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,lists);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }
    @RequestMapping("/delete")
    public Result delete(Integer id){ // @RequestBody从请求体内取数据
        try {
            travelGroupService.delete(id);
            return new Result(true,MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }
    @RequestMapping("/edit")
    public Result edit(Integer[] travelItemIds,@RequestBody TravelGroup travelGroup){ // @RequestBody从请求体内取数据
        try {
            travelGroupService.edit(travelItemIds,travelGroup);
            return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_TRAVELGROUP_FAIL);
        }


    }
    @RequestMapping("/getTravelItemIdsByTravelGroupId")
    public Result getTravelItemIdsByTravelGroupId(Integer travelGroupId){ // @RequestBody从请求体内取数据
        try {
            List<Integer> travelItemIds =  travelGroupService.getTravelItemIdsByTravelGroupId(travelGroupId);
            return new Result(true,"根据跟团游查询自由行成功",travelItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"根据跟团游查询自由行失败");
        }

    }
    @RequestMapping("/getById")
    public Result getById(Integer id){ // @RequestBody从请求体内取数据
        try {
            TravelGroup travelGroup = travelGroupService.getById(id);
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean  queryPageBean){ // @RequestBody从请求体内取数据
        PageResult pageResult = travelGroupService.findPage(queryPageBean.getCurrentPage(),
              queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
      return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody TravelGroup travelGroup,Integer[] travelItemIds){ // @RequestBody从请求体内取数据
        try {
            travelGroupService.add(travelGroup,travelItemIds);
            return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_TRAVELGROUP_FAIL);

        }
    }
}
